package com.pe1n.yangtaopai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.MyActivityItemOneRecyclerAdapter;
import com.pe1n.yangtaopai.bean.IntemOneListBean;

import com.pe1n.yangtaopai.presenter.PresenterIntemOne;
import com.pe1n.yangtaopai.utils.URLConstant;
import com.pe1n.yangtaopai.view.interfacese.IViewIntemOne;

import java.util.ArrayList;
import java.util.List;

public class ItemOneListActivity extends AppCompatActivity implements View.OnClickListener,IViewIntemOne{
    private Context mContext=this;
    private List<IntemOneListBean.DataBean.ItemsBean> myBeanList;
    private PresenterIntemOne mPresenterIntemOne;
    private ImageView imageView_OneitemActivity_lift,imageView_itemActivity_right,imageViewOne_itemActivity;
    private TextView textView_OneitemActivity_center,textview_activity_header,textview_activity_headernext;
    private XRecyclerView xRecyclerView_OneitemActivity;
    //
    private MyActivityItemOneRecyclerAdapter myActivityItemOneRecyclerAdapter;
    private ProgressBar progressBar_OneitemActivity;
    //设置隐藏和显示
    private RelativeLayout relativelayout_OneitemActivity;
    private GridLayoutManager gridLayoutManager;
    private String StringTextPath;//title
    private String ImageOneID;//id
    private String strUrl;//
    private View header;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_one_list);
        StringTextPath=getIntent().getStringExtra("OneImgTitle");
        ImageOneID=getIntent().getStringExtra("ID");
        if (ImageOneID.equals("")) {
            ImageOneID = "56";
        }
        int id=Integer.parseInt(ImageOneID);
        strUrl=String.format(URLConstant. LIST_ITEM_ONE_URL,id);
        String name=getIntent().getStringExtra("name");
        initView();
    }

    private void initView() {
        myBeanList=new ArrayList<>();
        mPresenterIntemOne = new PresenterIntemOne(this);
        myActivityItemOneRecyclerAdapter=new MyActivityItemOneRecyclerAdapter(mContext,myBeanList);
        progressBar_OneitemActivity = (ProgressBar) findViewById(R.id.progressbar_item_activity);
        //mPresenterIntemOne.LoadImageDate(URLConstant.LIST_URL);
        mPresenterIntemOne.LoadItemListOneData(strUrl);
        //Toast.makeText(this, ""+StringImagePath, Toast.LENGTH_SHORT).show();
        //返回键
        imageView_OneitemActivity_lift= (ImageView) findViewById(R.id.imageview_lift_item_fragment_activity);
        //弹出popo文斗 使用第三方分享
        imageView_itemActivity_right= (ImageView) findViewById(R.id.imageview_right_item_fragment_activity);

        /*//头布局
        imageViewOne_itemActivity= (ImageView) findViewById(R.id.imageview_item_list_one_avtivity);
        textview_activity_header= (TextView) findViewById(R.id.textview_item_list_one_activity);
        textview_activity_headernext= (TextView) findViewById(R.id.textview_item_list_nextone_activity);*/

        //传到的数据从 上边的文字
        textView_OneitemActivity_center= (TextView) findViewById(R.id.textview_center_item_fragment_activity);
        xRecyclerView_OneitemActivity= (XRecyclerView) findViewById(R.id.xrccyclerview_item_activity);
        xRecyclerView_OneitemActivity.setLayoutManager(new LinearLayoutManager(mContext));
        xRecyclerView_OneitemActivity.setAdapter(myActivityItemOneRecyclerAdapter);
        //设置隐藏显示
        relativelayout_OneitemActivity= (RelativeLayout) findViewById(R.id.relativelayout_item_activity);
        //View header =   LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);
        //mRecyclerView.addHeaderView(header);

        imageView_OneitemActivity_lift.setOnClickListener(this);
        imageView_itemActivity_right.setOnClickListener(this);
        //Intent 传来的东西
       // String StringImagePath=getIntent().getStringExtra("OneImgPath");
       // Glide.with(mContext).load(StringImagePath).placeholder(R.drawable.place_holder).into(imageViewOne_itemActivity);
        textView_OneitemActivity_center.setText(StringTextPath);
        //textView_OneitemActivity_center.setText();

        //头布局
        header =   LayoutInflater.from(this).inflate(R.layout.item_list_heder_layout, (ViewGroup)findViewById(android.R.id.content),false);
        xRecyclerView_OneitemActivity.addHeaderView(header);
        //relativelayout_OneitemActivity.setVisibility(View.INVISIBLE);
       /* gridLayoutManager=new GridLayoutManager(mContext,2);
        xRecyclerView_OneitemActivity.setLayoutManager(gridLayoutManager);
        xRecyclerView_OneitemActivity.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(gridLayoutManager.findFirstVisibleItemPosition()==0&&(int)recyclerView.getChildAt(0).getY()==0){
                    relativelayout_OneitemActivity.setVisibility(View.VISIBLE);
                }
            }
        });*/
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.imageview_lift_item_fragment_activity:
                finish();
                break;
            case R.id.imageview_right_item_fragment_activity:
                //弹出框框然后是第三方的分享
                Toast.makeText(this, "第三方的分享，快点做！", Toast.LENGTH_SHORT).show();
                break;

        }
    }

    @Override
    public void showProgress() {
        progressBar_OneitemActivity.setVisibility(View.GONE);
    }

    @Override
    public void showLoadFailMsg() {

    }

    @Override
    public void LoadItemListOneData(IntemOneListBean intemOneListBean) {
        myBeanList.addAll( intemOneListBean.getData().getItems());
        ((TextView)header.findViewById(R.id.textview_item_list_activity)).setText(intemOneListBean.getData().getTitle());
        ((TextView)header.findViewById(R.id.textview_item_list_nextactivity)).setText(intemOneListBean.getData().getIntro());
        Glide.with(mContext).load(intemOneListBean.getData().getImagePath())
                .into(((ImageView) header.findViewById(R.id.imageview_item_list_avtivity)));
        myActivityItemOneRecyclerAdapter.notifyDataSetChanged();
    }
}
